package com.imessage.utils;

import com.imessage.comm.Query;
import com.imessage.type.EnumMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@Slf4j
public class SqlUtil {



    public static String getQuerySql(
            Map<String,String> aliasMap, List<Query> queryList, int andcount){
        StringBuffer sqlsb=new StringBuffer();
        for(Query query:queryList){
            if(andcount>0){
                sqlsb.append(" and ");
            }else{
                sqlsb.append(" where ");
            }
            String alias=aliasMap.get(query.getTable());
            sqlsb.append(alias);
            sqlsb.append(".");
            sqlsb.append(query.getSql());
            andcount++;
        }
        return sqlsb.toString();
    }

    public static String getQuerySql(
            Map<String,String> aliasMap, Query query,int andcount){
        StringBuffer sqlsb=new StringBuffer();
        if(andcount>0){
            sqlsb.append(" and ");
        }else{
            sqlsb.append(" where ");
        }
        String alias=aliasMap.get(query.getTable());
        sqlsb.append(alias);
        sqlsb.append(".");
        sqlsb.append(query.getSql());
        return sqlsb.toString();
    }

    private static void setAliasColumn(Class clz,String alias,Map<String,String> selectMap,StringBuffer aliassb) {
        Field[] fields = clz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Column columnConfig = fields[i].getAnnotation(Column.class);
            if (columnConfig == null) {
                continue;
            }
            String colname = columnConfig.name();
            String aliascolname=colname;
            if (selectMap!=null
                    &&selectMap.size() > 0) {
                if (!selectMap.containsKey(colname)) {
                    continue;
                }
                aliascolname= selectMap.get(colname);
            }
            aliassb.append(alias);
            aliassb.append(".");
            aliassb.append(colname);
            aliassb.append(" as ");
            aliassb.append(alias);
            aliassb.append("__");
            aliassb.append(aliascolname);
            aliassb.append(",");
        }
    }

    public static String[] getAliasAndColum(String key){
       return key.split("__");
    }


    public static String getAliasColumn(Class clz,String alias,
                                        Map<String,String> selectMap,String end){
        StringBuffer aliassb=new StringBuffer();
        setAliasColumn(clz, alias,selectMap, aliassb);
        setAliasColumn(clz.getSuperclass(), alias,selectMap, aliassb);
        if(StringUtils.isNotBlank(end)){
            return aliassb.toString();
        }
        return aliassb.toString().substring(0, aliassb.toString().length()-1);
    }


    public static String replaceAndfirstToUpper(String srcStr, String org, String ob) {
        String newString = "";
        int first = 0;
        while (srcStr.indexOf(org) != -1) {
            first = srcStr.indexOf(org);
            if (first != srcStr.length()) {
                newString = newString + srcStr.substring(0, first) + ob;
                srcStr = srcStr.substring(first + org.length(), srcStr.length());
                srcStr = firstCharacterToUpper(srcStr);
            }
        }
        newString = newString + srcStr;
        return newString;
    }

    public static String firstCharacterToUpper(String srcStr) {
        return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);
    }


    public static void setDbObject(Class clz,Object object,String name,Object value){
        try{
            Field field=null;
            if(name.equalsIgnoreCase("id")
                   ||name.equalsIgnoreCase("createtime")
                    ||name.equalsIgnoreCase("updatetime")){
                field=clz.getSuperclass().getDeclaredField(name);
            }else{
                field= clz.getDeclaredField(name);
            }
            if(EnumMessage.class.isAssignableFrom(field.getType())){
                Method fromMethod=field.getType().getMethod("fromValue", String.class);
                Object fromObject=fromMethod.invoke(null,value);
                Method m=clz.getMethod(
                        "set"+SqlUtil.firstCharacterToUpper(name), field.getType());
                m.invoke(object,fromObject);
            }else {
                Method m=clz.getMethod(
                        "set"+SqlUtil.firstCharacterToUpper(name),field.getType());
                m.invoke(object,value);
            }
        }catch (Exception e){
            log.error(clz.getName(), e);
        }
    }








}
